$(document).ready(function(){
	var myurl=location.href; 
	var formid=myurl.split("/");
	var id=formid[formid.length-1];
	$.ajax
	   (
	      {
		    type : "POST",
		    url : "/mcipback/statistics/showResult/",
		    data:
		    {
		    	"tableid" : id,				
		    },
		    success : function(data) 
		    {
		    	//console.log(data);
		    	//var tabledata=eval('('+data+')');
		    	var flag=0;//存储用户数据是否为空，0为非空，1为空
		    	var tabledata=data;
		    	var titles=new Array();
		    	console.log(tabledata)
		    	for(var key in tabledata[0]){
		    		//console.log(key);
		    		if(key=="column_name")
		    			{
		    				flag=1;
		    				break;
		    			}
		    		else
		    			{
		    	    		titles.push(key);
		    			}
		    	}
		    	$("#title").append("<th>编号</th>");
		    	if(flag==1)
		    		{
		    			for(var x=1;x<tabledata.length;x++)
		    				{
		    					var colname=tabledata[x].column_name;
		    					$("#title").append("<th>"+colname+"</th>");
		    				}
		    		}
		    	else
		    	{
		    	var len=titles.length;
		    	//第一个key是id，不需要
		    	console.log(len);
		    	for(i=1;i<len;i++)
		    		{
		    			$("#title").append("<th>"+titles[i]+"</th>");
		    		};
		    	var datalen=tabledata.length;
		    	for(k=0;k<datalen;k++)
		    		{
		    			var str="";
		    			for(j=1;j<len;j++)
		    				{
		    					var title=titles[j];				
		    						str+="<td>"+tabledata[k][title]+"</td>";	    					
		    				};
		    			$("#infos").append("<tr><td>"+(k+1)+"</td>"+str+"</tr>");
		    		};}
		    	    $('#result').DataTable({
		    	           language: {
		    	           	"decimal": ",",
		    	               "sProcessing": "处理中...",
		    	               "sSearchPlaceholder":"",
		    	               "sLengthMenu": "显示 _MENU_ 每页",
		    	               "sZeroRecords": "没有匹配结果",
		    	               "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
		    	               "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
		    	               "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
		    	               "sInfoPostFix": "",
		    	               "sSearch": "搜索:",
		    	               "sUrl": "",
		    	               "sEmptyTable": "表中数据为空",
		    	               "sLoadingRecords": "载入中...",
		    	               "sInfoThousands": ",",
		    	               "oPaginate": {
		    	                   "sFirst": "首页",
		    	                   "sPrevious": "上页",
		    	                   "sNext": "下页",
		    	                   "sLast": "末页"
		    	               },
		    	               "oAria": {
		    	                   "sSortAscending": ": 以升序排列此列",
		    	                   "sSortDescending": ": 以降序排列此列"
		    	               }
		    	           },
		    	           bProcessing: true,
		    		       ordering:  false,
		    		       buttons: [{
		    		          extend: 'excelHtml5',
		    		          text: '导出Excel',
		    	   	          title:"信息采集表"+new Date().getFullYear()+parseInt(new Date().getMonth()+1)+new Date().getDate(),
		    		       }],
		    		       initComplete:function(settings,json){
		    		            	$(this).DataTable().buttons().container().appendTo($("#excel"));
		    		            	$(".dataTables_filter").parent().prev().css("width","auto");
		    		            },
		    	    });

		    	    jQuery('#sample_'+i+'_wrapper .dataTables_filter input').addClass("form-control"); // modify table search input
		    	    jQuery('#sample_'+i+'_wrapper .dataTables_length select').addClass("form-control"); // modify table per page dropdown
		    }
		    
		    }
	      )

})
